package com.ityun.ogapp

import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import com.ityun.ogapp.base.BaseActivity
import com.ityun.ogapp.databinding.ActivityMainBinding
import com.ityun.ogapp.fragment.AppFragment
import com.ityun.ogapp.fragment.ContactFragment
import com.ityun.ogapp.fragment.MineFragment
import com.ityun.ogapp.wechat.WechatManager
import com.ityun.ogapp.wechat.WechatVideoManager
import com.ven.assists.AssistsCore
import com.ven.assists.stepper.StepManager
import kotlinx.coroutines.launch

class MainActivity : BaseActivity<ActivityMainBinding>() {
    private val contactFragment by lazy { ContactFragment() }
    private val appFragment by lazy { AppFragment() }
    private val mineFragment by lazy { MineFragment() }
    private var currentFragment: Fragment = contactFragment
    override fun createViewBinding(): ActivityMainBinding {
        return ActivityMainBinding.inflate(layoutInflater)
    }

    override fun initViews() {
        supportFragmentManager.beginTransaction()
            .add(R.id.fl_layout, contactFragment)
            .commit()

        // 设置Tab点击事件
        mBinding.tabContact.setOnClickListener {
            switchFragment(contactFragment)
            updateTabSelection(0)
        }

        mBinding.tabApp.setOnClickListener {
            switchFragment(appFragment)
            updateTabSelection(1)
        }

        mBinding.tabMine.setOnClickListener {
            switchFragment(mineFragment)
            updateTabSelection(2)
        }
        // 默认选中第一个Tab
        updateTabSelection(0)
//        if (!AssistsCore.isAccessibilityServiceEnabled()) {
//            AssistsCore.openAccessibilitySetting()
//        } else {
//            lifecycleScope.launch {
////                val wechatManager = WechatManager()
////// 获取步骤收集器并执行
////                val collector = StepCollector("com.tencent.mm")
////                wechatManager.onImpl(collector)
////                StepManager.execute(WechatVideoManager::class.java.name, 1, data="妈妈1111",begin = true)
////                WechatManager.openWechatVideo("妈妈")
//            }
//        }

    }

    private fun switchFragment(targetFragment: Fragment) {
        if (currentFragment != targetFragment) {
            val transaction = supportFragmentManager.beginTransaction()
            if (!targetFragment.isAdded) {
                transaction.hide(currentFragment)
                    .add(R.id.fl_layout, targetFragment)
                    .commit()
            } else {
                transaction.hide(currentFragment)
                    .show(targetFragment)
                    .commit()
            }
            currentFragment = targetFragment
        }
    }

    private fun updateTabSelection(index: Int) {
        mBinding.tabContact.isSelected = index == 0
        mBinding.tabApp.isSelected = index == 1
        mBinding.tabMine.isSelected = index == 2
    }
}
